﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="设备附件.aspx.cs" Inherits="syc.Jidong.设备附件" %>
<html>
<head>
<title></title>
<style>
#RowList{display:none;}
</style>
</head>
<body>
<script type="text/javascript" language="javascript" src="/common.js"></script>
<form id="form1" runat="server"  onsubmit="return(checkInput())">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
 InsertCommand="insert into 设备附件(台账ID,名称,型号,单位,单价,数量,备注)values(@ID,@名称,@型号,@单位,@单价,@数量,@备注);"
 DeleteCommand="delete from 设备附件 where 台账ID=@ID;"
ConnectionString="<%$ ConnectionStrings:SCMConnectionString1 %>">
<InsertParameters>
<asp:Parameter Name="ID" Size="4" DbType="Int32"/>
<asp:Parameter Name="名称" DbType="String"  Size="40"/>
<asp:Parameter Name="型号" DbType="AnsiString" Size="20"/>
<asp:Parameter Name="单位" DbType="AnsiString" Size="10"/>
<asp:Parameter Name="单价" DbType="Decimal" Size="9"/>
<asp:Parameter Name="数量" Size="4" DbType="Int32"/>
<asp:Parameter Name="备注" DbType="String"  Size="40"/>
</InsertParameters>
<DeleteParameters>
<asp:Parameter Name="ID" Size="4" DbType="Int32"/>
</DeleteParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="RowList" runat="server" DataSourceID="SqlDataSource1" DataTextField="val" DataValueField="txt" >
</asp:DropDownList>

<asp:HiddenField ID="Cell1" runat="server" Value="" />
<asp:HiddenField ID="Cell2" runat="server" Value="" />
<asp:HiddenField ID="Cell3" runat="server" Value="" />
<asp:HiddenField ID="Cell4" runat="server" Value="" />
<asp:HiddenField ID="Cell5" runat="server" Value="" />
<asp:HiddenField ID="Cell6" runat="server" Value="" />

<asp:Label ID="BillID" runat="server" Text="" Visible="false"></asp:Label>
<div id="wintop" class="wintop">固定资产附件</div>
<asp:Panel ID="Panel1" runat="server" CssClass="panel">
    <asp:Button ID="Button_Save" runat="server" Text="保 存" UseSubmitBehavior="false" 
        CssClass="button" onclick="Button_Save_Click" />
    
    <input type="button" value="计算金额" class="button" onclick="calc();" />
    
    <input type="button" value="追加一行" class="button" onclick="addrow('');" />
    
    <input type="button" value="导出Excel" class="button" onclick="Export();" />

    <input type="button" value="关 闭" class="button" onclick="parent.swin2_close();" />
</asp:Panel>

<asp:Panel ID="Panel_Msg" runat="server" CssClass="panel2" Visible="false">
    <asp:Label ID="Message" runat="server" Text="" CssClass="prompt"></asp:Label>
</asp:Panel>
<div style="text-align:center">
<script>putBillHead();</script>
<asp:Panel ID="Panel5" runat="server">

<style>
tr{height:16pt;}
td.nm{border:.5pt solid #000000;font-size:10pt;text-align:center;}
td.nb{border:none;font-size:10pt;text-align:center;}
div{word-break:break-all;}
</style>
<table id=mt border=0 cellpadding=0 style=border-collapse:collapse>
<tr style=height:20pt>
<td class=nm style=width:40pt;>序号</td>
<td class=nm style=width:90pt;>名称</td>
<td class=nm style=width:80pt;>型号规格</td>
<td class=nm style=width:40pt;>单位</td>
<td class=nm style=width:40pt;>数量</td>
<td class=nm style=width:50pt;>单价(元)</td>
<td class=nm style=width:60pt;>金额(元)</td>
<td class=nm style=width:80pt;>备注</td>
</tr>

<tr>
<td class=nm>合计</td>
<td class=nm></td>
<td class=nm></td>
<td class=nm></td>
<td class=nm></td>
<td class=nm></td>
<td class=nm></td>
<td class=nm></td>
</tr>
</table>


</asp:Panel>
<script>putBillFoot();</script>
</div>
</form>

<form id="form2" method="post" action="/PageExport.ashx">
<input type="hidden" name="filename" value="设备附件.xls" />
<input type="hidden" name="content" value="" />
</form>


<script>
var mt = document.getElementById("mt");
var rows = 0;

document.onkeydown = function(e) {
    if (!e) e = window.event;
    var tag = e.srcElement;
    if (!tag) tag = e.target;
    if (tag.tagName != "DIV") return;
    if (e.keyCode == 13) {
        if (tag.parentNode.parentNode.rowIndex == rows && tag.parentNode.cellIndex == 7) {
            addrow("");
        }
        e.keyCode = 9;
    }

}

function calc() {
    var row;
    var n, p;
    var total = 0;
    for (var i = 1; i < mt.rows.length - 1; i++) {
        row = mt.rows[i];
        n = Number(row.cells[4].innerText || row.cells[4].textContent);
        if (n == NaN) {
            n = -1;
        }

        p = Number(row.cells[5].innerText || row.cells[5].textContent);
        if (p == NaN) {
            p = -1;
        }
        if (n > 0 && p > 0) {
            row.cells[6].innerHTML = n * (p * 1000) / 1000;
            total += n * p;
        }
        else {
            row.cells[6].innerHTML = "";
        }
    }
    mt.rows[mt.rows.length - 1].cells[6].innerHTML = total;
}

function addrow(s) {
    rows++;
    var list = s.split("|=*");
    for (var i = 0; i < 6; i++) {
        if (list[i] == null)list[i] = "";
    }
    if (list[3] == -1) list[3] = "";
    if (list[4] == -1) list[4] = "";
    var row = mt.insertRow(rows);
    var col = row.insertCell(-1);
    col.className = "nm";
    col.innerHTML = rows;
    
    col = row.insertCell(-1);
    col.className = "nm";
    col.innerHTML = "<div tabindex=0 contentEditable style=width:90pt;>" + list[0] + "</div>";
    
    col = row.insertCell(-1);
    col.className = "nm";
    col.innerHTML = "<div tabindex=0 contentEditable style=width:80pt;>" + list[1] + "</div>";
    
    col = row.insertCell(-1);
    col.className = "nm";
    col.innerHTML = "<div tabindex=0 contentEditable style=width:40pt;>" + list[2] + "</div>";
    
    col = row.insertCell(-1);
    col.className = "nm";
    col.innerHTML = "<div tabindex=0 contentEditable style=width:40pt;>" + list[3] + "</div>";
    
    col = row.insertCell(-1);
    col.className = "nm";
    col.innerHTML = "<div tabindex=0 contentEditable style=width:50pt;>" + list[4] + "</div>";

    col = row.insertCell(-1);
    var n, p;
    n = Number(list[3]);
    if (n == NaN) {
        n = -1;
    }

    p = Number(list[4]);
    if (p == NaN) {
        p = -1;
    }

    if (n > 0 && p > 0) col.innerHTML = n * (p * 1000) / 1000;
    else col.innerHTML = "";
    col.className = "nm";
    
    col = row.insertCell(-1);
    col.className = "nm";
    col.innerHTML = "<div tabindex=0 contentEditable style=width:80pt;>" + list[5] + "</div>";
}

var RowList = document.getElementById("RowList");
for (var i = 0; i < RowList.options.length; i++) {
    addrow(RowList.options[i].text);
}
if (rows == 0) addrow("");
calc();


//输入检测
function checkInput() {
    var txt;
    var Cell1 = document.getElementById("Cell1");
    var Cell2 = document.getElementById("Cell2");
    var Cell3 = document.getElementById("Cell3");
    var Cell4 = document.getElementById("Cell4");
    var Cell5 = document.getElementById("Cell5");
    var Cell6 = document.getElementById("Cell6");
    Cell1.value = "";
    Cell2.value = "";
    Cell3.value = "";
    Cell4.value = "";
    Cell5.value = "";
    Cell6.value = "";
    var n, p;
    
    for (var i = 1; i < mt.rows.length-1; i++) {
        txt = mt.rows[i].cells[1].innerText;

        if (txt==null) txt = mt.rows[i].cells[1].textContent;
        txt = txt.replace(/^\s+/, '').replace(/\s+$/, '');
        
        if (txt == "") continue;
        Cell1.value += "|=*" + txt;

        txt = mt.rows[i].cells[2].innerText;
        if (txt == null) txt = mt.rows[i].cells[2].textContent;
        txt = txt.replace(/^\s+/, '').replace(/\s+$/, '');
        Cell2.value += "|=*" + txt;

        txt = mt.rows[i].cells[3].innerText;
        if (txt == null) txt = mt.rows[i].cells[3].textContent;
        txt = txt.replace(/^\s+/, '').replace(/\s+$/, '');
        Cell3.value += "|=*" + txt;

        txt = mt.rows[i].cells[4].innerText;
        if (txt == null) txt = mt.rows[i].cells[4].textContent;
        txt = txt.replace(/^\s+/, '').replace(/\s+$/, '');
        
        
        n = Number(txt);
        if (n == NaN) {
            n = -1;
            mt.rows[i].cells[4].innerHTML = "<div tabindex=0 contentEditable style=width:40pt;></div>";
        }
        else {
            n = Math.floor(n);
            mt.rows[i].cells[4].innerHTML = "<div tabindex=0 contentEditable style=width:40pt;>" + n + "</div>";
        }
        Cell4.value += "|=*" + n;
        
        txt = mt.rows[i].cells[5].innerText;
        if (txt == null) txt = mt.rows[i].cells[5].textContent;
        txt = txt.replace(/^\s+/, '').replace(/\s+$/, '');
        Cell5.value += "|=*" + txt;
        p = Number(txt);

        p = Number(txt);
        if (p == NaN) {
            p = -1;
        }
        
        txt = mt.rows[i].cells[7].innerText;
        if (txt == null) txt = mt.rows[i].cells[7].textContent;
        txt = txt.replace(/^\s+/, '').replace(/\s+$/, '');
        Cell6.value += "|=*" + txt;

        if (n > 0 && p > 0) mt.rows[i].cells[6].innerHTML = n * (p * 1000) / 1000;
        else mt.rows[i].cells[6].innerHTML = "";
    }
}

//导出Excel
function Export() {
    form2.content.value =
"<html xmlns:o='urn:schemas-microsoft-com:office:office'\nxmlns:x='urn:schemas-microsoft-com:office:excel'\nxmlns='http://www.w3.org/TR/REC-html40'>\n<head>\n<meta http-equiv=Content-Type content='text/html; charset=utf-8'>\n<style>\n<!--table\n@page\n{mso-header-data:;\nmso-footer-data:;\nmargin:.79in .31in .79in .31in;\nmso-header-margin:.51in;\nmso-footer-margin:.51in;\nmso-horizontal-page-align:center;\n}\n-->\n</style>\n<!--[if gte mso 9]><xml>\n<x:ExcelWorkbook>\n<x:ExcelWorksheets>\n<x:ExcelWorksheet>\n<x:Name>设备附件</x:Name>\n<x:WorksheetOptions>\n<x:DefaultRowHeight>285</x:DefaultRowHeight>\n<x:Print>\n<x:ValidPrinterInfo/>\n<x:Scale>100</x:Scale>\n<x:PaperSizeIndex>9</x:PaperSizeIndex>\n<x:HorizontalResolution>200</x:HorizontalResolution>\n<x:VerticalResolution>200</x:VerticalResolution>\n</x:Print>\n<x:Selected/>\n<x:Panes>\n<x:Pane>\n<x:Number>3</x:Number>\n<x:ActiveRow>0</x:ActiveRow>\n<x:ActiveCol>0</x:ActiveCol>\n</x:Pane>\n</x:Panes>\n</x:WorksheetOptions>\n</x:ExcelWorksheet>\n</x:ExcelWorkbook>\n</xml><![endif]-->\n</head>\n<body>"
            + document.getElementById("Panel5").innerHTML.replace(/<div [^>]*>/gi, "").replace(/<\/div>/gi, "");
+"</body></html>";
    form2.submit();
}


</script>
</body>
</html>
